//
//  ICShareViewController.m
//  ICome
//
//  Created by ENN on 16/9/5.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICShareViewController.h"
#import "ICChatForwardCell.h"
#import "ICShareSearchController.h"
#import "ICReceiveShareObject.h"
#import "ICShareBottomView.h"
#import "ICShareTextView.h"
#import "ICShareImageView.h"
#import "ICShareFileView.h"
#import "ICShareTextPicView.h"
#import "ICShareInstance.h"
#import "ICShareSuccessView.h"

@interface ICShareViewController ()<UISearchControllerDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,ICForwardSearchDelegate>
{
    ICShareBottomView *_shareBgView;
}

@property (nonatomic, strong) ICBaseSearchViewController *searchController;

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, weak) UITableView *tableView;

@end

@implementation ICShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupUI];
    
    [self loadData];
    
    [self addNotification];
}

- (void)setupNav
{
    self.title = @"分享至";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"关闭" itemType:UIBarButtonItemTypeLeft target:self action:@selector(cancelForwardMessage)];
}

- (void)setupUI
{
    ICShareSearchController *searchVC   = [[ICShareSearchController alloc] init];
    searchVC.delegate  =  self;
    
    ICBaseSearchViewController *searchController = [[ICBaseSearchViewController alloc] initWithSearchResultsController:searchVC];
    searchController.placeholder = @"搜索";
    searchController.delegate = self;
    searchController.searchBar.delegate  = self;
    searchController.searchResultsUpdater = searchVC;
    self.definesPresentationContext = YES;
    _searchController = searchController;  // 强指针引用
    self.tableView.tableHeaderView = searchController.searchBar;
    
    self.tableView.frame  = CGRectMake(0,0, self.view.width, self.view.height-64);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (void)loadData
{
    WEAKSELF
    [ICMessageDatabase getNormalGroups:^(NSArray *array, NSError *error) {
        weakSelf.dataArr = (NSMutableArray *)array;
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark - Notification

- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareCancel) name:ICShareCancelNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareConfirm:) name:ICShareConfirmNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareViewExited) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow
{
    if (_shareBgView) {
        UIView * subView = _shareBgView.subviews[0];
        [UIView animateWithDuration:1 animations:^{
            subView.y = subView.y - 80;
        }];
    }
}

- (void)keyboardWillHide
{
    if (_shareBgView) {
        UIView * subView = _shareBgView.subviews[0];
        [UIView animateWithDuration:1 animations:^{
            subView.y = subView.y + 80;
        }];
    }
}

// 取消分享
- (void)shareCancel
{
    if (_shareBgView) {
        [_shareBgView removeFromSuperview];
    }
}

// 确认分享
- (void)shareConfirm:(NSNotification *)notify
{
    if ([self.delegate respondsToSelector:@selector(shareViewController:confirmForShareData:)]) {
        [self.delegate shareViewController:self confirmForShareData:(ICShareInstance *)notify.object];
    }
}


- (void)shareViewExited
{
    if (_shareBgView) {
        [_shareBgView removeFromSuperview];
        _shareBgView = nil;
        _shareContent = nil;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - ICForwardSearchDelegate

- (void)groupSearch:(ICGroup *)group
{
    [self forwardMessage:group];
}

- (void)forwardMessage:(ICGroup *)group
{
    NSLog(@"-----%s",__func__);
    [self.searchController setActive:NO];
    [self share:group];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICChatForwardCell *cell = [ICChatForwardCell cellWithTableView:tableView];
    cell.group = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f * Level;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICGroup *group = self.dataArr[indexPath.row];
    [self share:group];
}

- (void)share:(ICGroup *)group {
    if (self.shareContent) {
        ICShareBottomView *bgView = [[ICShareBottomView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height)];
        [[[UIApplication sharedApplication].keyWindow.subviews lastObject] addSubview:bgView];
        _shareBgView = bgView;
        ICShareInstance *shareData = [[ICShareInstance alloc] init];
        // 分享对象
        ICReceiveShareObject *shareObject = [[ICReceiveShareObject alloc] initWithDict:_shareContent[@"shareObject"]];
        shareData.shareObject = shareObject;
        shareData.group        = group;
        bgView.shareData = shareData;
    }
}


#pragma mark - searchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            [cancel setTitleColor:ICSEARCHCANCELCOLOR forState:UIControlStateNormal];
        }
    }
}

#pragma mark - Event

- (void)cancelForwardMessage
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Getter

- (UITableView *)tableView
{
    if (nil == _tableView) {
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView = tableView;
    }
    return _tableView;
}




@end
